// 清除日志输出插件 15.5MB ==> 5.05MB
const TerserPlugin = require("terser-webpack-plugin")
// Gzip压缩插件
const CompressionWebpackPlugin = require('compression-webpack-plugin')
// 判断当前是否为生产环境
const isProduction = process.env.NODE_ENV === 'production';
let plugins = [];
let externals;
let CDN = {
    build: [
        'https://cdn.bootcdn.net/ajax/libs/vue/2.6.11/vue.min.js',
        'https://cdn.bootcdn.net/ajax/libs/vuex/3.6.2/vuex.min.js',
        'https://cdn.bootcdn.net/ajax/libs/vue-router/3.6.5/vue-router.min.js',
        'https://cdn.bootcdn.net/ajax/libs/axios/1.1.3/axios.min.js'
    ],
    dev: []
}
if (isProduction) {
    // 清除日志输出
    plugins.push(new TerserPlugin({
        terserOptions: {
            output: {
                comments: false,//去除注释
            },
            warnings: false,//去除黄色警告
            compress: {
                drop_console: true,
                drop_debugger: true,//特定情况利用debugger防止别人做反测试，隐藏源码
                pure_funcs: ['console.log'],//移除console.log和dir
            }
        }
    }))
    // Gzip打包压缩
    plugins.push(new CompressionWebpackPlugin({
        test: /\.(js|json|css)$/i,//图片一般不Gzip压缩 
        threshold: 10240 //只有在10kb以上才压缩
    }))
    // CDN处理
    externals = {
        vue: 'Vue',
        vuex: 'Vuex',
        'vue-router': 'VueRouter',
        axios: 'axios'
    }
}

module.exports = {
    // 关闭语法校验
    lintOnSave: false,
    publicPath: process.env.NODE_ENV == 'production' ? './' : '/',
    //如果是生产环境，则不需要打包生成map文件
    productionSourceMap: !isProduction,
    // 代理跨域
    configureWebpack: {
        devServer: {
            proxy: {
                '/api': {
                    target: 'http://175.178.124.20:8589',
                    pathRewrite: { '/api': '' },
                    ws: true,
                    changeOrigin: true
                }
            }
        },
        plugins,
        externals
    },
    // 再加工
    chainWebpack(config) {
        config.plugin('html').tap(args => {
            if (isProduction) {
                args[0].cdnLink = CDN.build;
            } else {
                args[0].cdnLink = CDN.dev;
            }
            return args
        })
    }
}